package codetop.microsoft.T179;

import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @Author: 18362
 * @Create: 2022-09-07 11:07:29 星期三
 */
class Solution {
    public String largestNumber(int[] nums) {
        String[] ss = new String[nums.length];
        for (int i = 0; i < nums.length; i++)
            ss[i] = String.valueOf(nums[i]);
        Arrays.sort(ss, (sa, sb) -> {
            int idx = 0, ia = 0, ib = 0;
            if (sa.equals(sb))
                return 0;
            while (idx < sa.length() + sb.length()) {
                if (sa.charAt(ia) != sb.charAt(ib))
                    return sb.charAt(ib) - sa.charAt(ia);
                ia++;
                ib++;
                idx++;
                if (ia == sa.length())
                    ia = 0;
                if (ib == sb.length())
                    ib = 0;
            }
            return 0;
        });
        if (ss[0].equals("0"))
            return "0";
        StringBuilder sb = new StringBuilder();
        for (String s : ss)
            sb.append(s);
        return sb.toString();
    }
}